COSC 220 Computer Science II

 

Sorted Singly Linked List with Template


In this lab, you will implement a sorted (by the nodeValue) singly linked list . You need divide your program into three file: link.h, linkFun.cpp and linkMain.cpp

TASK 1: Implement link.h: A header file for a node data structure and prototypes of all the functions to be implemented in task 2. Make sure to guard your header file.

node Structure should be similar to the following:

template <typename T>

class node{

      public: 

T nodeValue;

node<T>* next;

node():nodeValue(0), next(NULL){ } // default constructor to set nodeValue to be zero and set next to be NULL

node(const T& item, node *nextNode=NULL): nodeValue(item),next(nextNode) { } // constructor to set nodeValue to be newValue and set next to nextNode

};

TASK 2: Implement linkFun.cpp: an implementation file for all free functions to manipulate a sorted singly linked list.

Free functions to be implemented are:

CreateNode:

Parameters: a value to be placed in the node

Return Type: pointer to Node

Task: Create an object of node using "new" operator and set the "nodeValue" of this new node to

be the input parameter and the "next" to be NULL.

template <typename T>

node<T>* CreateNode(const T &value );

 

InsertNode:

Parameters: pointer to the first node of a linked list, the value of the new node to be inserted.

Return Type: None

Task: insert a new node to the linked list (sorted by nodeValue of each node in ascending order)

You need to consider the following cases for inserting a new node to the linked list.

- empty linked list

- Insert at the front of the list

- insert between two nodes

template <typename T>

void InsertNode(node<T>* &front, const T & value );

SearchNode:

Parameters: pointer to the first node of a linked list, a value that matches the "nodeValue" of the node to be searched.

Return Type: pointer to the node whose "nodeValue" matches the value to be searched or NULL if node is not found

Task: Search a node by value

template <typename T>

node<T>* SearchNode(node<T>* front, const T & value );

 

DeleteTarget:

Parameters: pointer to the first node of a linked list, value of the node to be deleted.

Return Type: None.

Task: Remove a given node from the linked list.

You need to consider following cases for deleting from the linked list:

- empty linked list

- delete at the front of the list

- delete between two nodes (including the last node).

template <typename T>

void DeleteNode(node<T>* &front, const T & value );

 

PrintList:

Parameters: pointer to the first node of a linked list and a string used to separate the value of each node.

Return Type: None

Task: Display the value of all the nodes in the linked list.

template <typename T>

void PrintList(node<T>* front, const string & separator = " ");

TASK 3: linkMain.cpp: a program with the main function for displaying menu, taking input from users, manipulating a linked list by adding nodes to the list (front, back, middle), deleting from the list (front, back, middle), constantly displaying and searching.

Main:

Parameters: None

Return Type: int

Task: Menu driven program to manipulate a linked list of integers

Menu should provide the following items:

- insert a node

- delete a node

- print the list

- search a node and print its value

- quit the program

 

You should provide validation for user menu input.

 

#include "link.h"

#include "linkFun.cpp"

int main(){

node<int> *front = NULL;

// fill your code here

 

return 0;

}

WHAT TO SUBMIT TO YOUR INSTRUCTOR: